package com.techwells.applicationMarket.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.techwells.applicationMarket.dao.CurrencyMapper;
import com.techwells.applicationMarket.domain.Currency;
import com.techwells.applicationMarket.service.CurrencyService;
import com.techwells.applicationMarket.util.PagingTool;
import com.techwells.applicationMarket.util.ResultInfo;

/**
 *  币种的业务层的实现类
 * @author 陈加兵
 */
@Service
public class CurrencyServiceImpl implements CurrencyService{
	
	@Resource
	private CurrencyMapper currencyMapper;
	
	
	@Override
	public Object addCurrency(Currency currency) throws Exception {
		ResultInfo resultInfo=new ResultInfo();
		int count=currencyMapper.insertSelective(currency);
		if (count==0) {
			resultInfo.setCode("-1");
			resultInfo.setMessage("添加失败");
			return resultInfo;
		}
		resultInfo.setMessage("添加成功");
		return resultInfo;
	}

	
	@Override
	public Object getCurrencyById(Integer currencyId) throws Exception {
		ResultInfo resultInfo=new ResultInfo();
		Currency currency=currencyMapper.selectByPrimaryKey(currencyId);
		
		if (currency==null) {
			resultInfo.setCode("-1");
			resultInfo.setMessage("该 币种不存在");
			return resultInfo;
		}
		resultInfo.setMessage("获取成功");
		resultInfo.setResult(currency);
		return resultInfo;
	}

	
	@Override
	public Object modifyCurrencyReturnObject(Currency currency) throws Exception {
		ResultInfo resultInfo=new ResultInfo();
		int count=currencyMapper.updateByPrimaryKeySelective(currency);
		if (count==0) {
			resultInfo.setCode("-1");
			resultInfo.setMessage("修改失败");
			return resultInfo;
		}
		resultInfo.setMessage("修改成功");
		return resultInfo;
	}
	
	
	@Override
	public int modifyCurrencyReturnCount(Currency currency) throws Exception {
		return currencyMapper.updateByPrimaryKeySelective(currency);
	}

	
	@Override
	public Object deleteCurrencyReturnObject(Integer currencyId) throws Exception {
		ResultInfo resultInfo=new ResultInfo();
		int count=currencyMapper.deleteByPrimaryKey(currencyId);
		
		if (count==0) {
			resultInfo.setCode("-1");
			resultInfo.setMessage("删除失败");
			return resultInfo;
		}
		resultInfo.setMessage("删除成功");
		return resultInfo;
	}
	
	
	@Override
	public int deleteCurrencyReturnCount(Integer currencyId) throws Exception {
		return currencyMapper.deleteByPrimaryKey(currencyId);
	}

	
	@Override
	public Object getCurrencyList(PagingTool pagingTool) throws Exception {
		ResultInfo resultInfo=new ResultInfo();
		
		int total=currencyMapper.countTotal(pagingTool);
		if (total==0) {
			resultInfo.setMessage("获取成功");
			resultInfo.setResult(null);
			resultInfo.setTotal(total);
			return resultInfo;
		}
		
		
		List<Currency> currencies=currencyMapper.selectList(pagingTool);
		resultInfo.setMessage("获取成功");
		resultInfo.setResult(currencies);
		resultInfo.setTotal(total);
		return resultInfo;
	}

	
	
	
}